﻿namespace AppHtml.Control
{
    using BaseControl;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class Html
    {

        /// <summary>
        /// 将匿名对象转换为字典
        /// </summary>
        /// <param name="Attribute"></param>
        /// <returns></returns>
        public Dictionary<string, string> ObjectToDictionary(object Attribute)
        {
            var di = new Dictionary<string, string>();

            Type ty = Attribute.GetType();

            var fields = ty.GetProperties().ToList();

            foreach (var item in fields)
            {
                var Name = "";
                if (item.Name.Contains("_"))
                    Name = item.Name.Replace("_", "-");
                else
                    Name = item.Name;

                di.Add(Name, item.GetValue(Attribute).ToString());
            }

            return di;
        }

        /// <summary>
        /// Input
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Name"></param>
        /// <param name="Placeholder"></param>
        /// <param name="Col"></param>
        /// <param name="Type"></param>
        /// <returns></returns>
        public string Input(string Title, string Name, string Placeholder, object Attribute = null, int Col = 6, string Type = "text")
        {
            //<div class="col-sm-6 col-md-4">
            //    <h4 class="example-title">会员名称</h4>
            //    <input type="text" class="form-control" name="Member_Name" data-bind="value:Member_Name" placeholder="请输入 会员名称" />
            //</div>

            var H4 = PageControl.H4(new Dictionary<string, string> { 
                {"class","example-title"}
            }, Title);

            var _Input_Attribute = new Dictionary<string, string>();
            _Input_Attribute.Add("type", Type);
            _Input_Attribute.Add("class", "form-control");
            _Input_Attribute.Add("name", Name);
            _Input_Attribute.Add("data-bind", "value:" + Name);
            _Input_Attribute.Add("Placeholder", (string.IsNullOrEmpty(Placeholder) ? "请输入 " + Title : Placeholder));

            if (Attribute != null)
            {
                foreach (var item in ObjectToDictionary(Attribute))
                {
                    _Input_Attribute[item.Key] = item.Value;
                }
            }

            var Input = PageControl.Input(_Input_Attribute);

            var _Div_Attribute = new Dictionary<string, string>();
            _Div_Attribute.Add("class", "col-sm-" + Col);
            return PageControl.Div(_Div_Attribute, H4 + Input);
        }

        /// <summary>
        /// Input
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Attribute"></param>
        /// <param name="Col"></param>
        /// <returns></returns>
        public string Input(string Title, object Attribute, int Col = 6)
        {
            //<div class="col-sm-6 col-md-4">
            //    <h4 class="example-title">会员名称</h4>
            //    <input type="text" class="form-control" name="Member_Name" data-bind="value:Member_Name" placeholder="请输入 会员名称" />
            //</div>

            var H4 = PageControl.H4(new Dictionary<string, string> { 
                {"class","example-title"}
            }, Title);
            var Input = PageControl.Input(ObjectToDictionary(Attribute));

            var _Div_Attribute = new Dictionary<string, string>();
            _Div_Attribute.Add("class", "col-sm-" + Col);
            return PageControl.Div(_Div_Attribute, H4 + Input);
        }

        /// <summary>
        /// Select
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Name"></param>
        /// <param name="FuncOpetion"></param>
        /// <param name="Placeholder"></param>
        /// <param name="Attribute"></param>
        /// <param name="Col"></param>
        /// <returns></returns>
        public string Select(string Title, string Name, Func<string> FuncOpetion, object Attribute = null, int Col = 6)
        {
            //<div class="col-sm-6 col-md-4">
            //    <h4 class="example-title">性别</h4>
            //    <select class="form-control" name="Member_Sex" data-bind="value:Member_Sex">
            //        <option value="">==请选择 性别==</option>
            //        @foreach (var item in sexList)
            //        {
            //            <option value="@item">@item</option>
            //        }
            //    </select>
            //</div>

            var Options = "<option value=\"\">==请选择 " + Title + "==</option>";
            Options += FuncOpetion();

            var H4 = PageControl.H4(new Dictionary<string, string> { 
                {"class","example-title"}
            }, Title);

            var _Select_Attribute = new Dictionary<string, string>();
            _Select_Attribute.Add("class", "form-control");
            _Select_Attribute.Add("name", Name);
            _Select_Attribute.Add("data-bind", "value:" + Name);

            if (Attribute != null)
            {
                foreach (var item in ObjectToDictionary(Attribute))
                {
                    _Select_Attribute[item.Key] = item.Value;
                }
            }

            var Select = PageControl.Select(_Select_Attribute, Options);

            var _Div_Attribute = new Dictionary<string, string>();
            _Div_Attribute.Add("class", "col-sm-" + Col);
            return PageControl.Div(_Div_Attribute, H4 + Select);
        }

        /// <summary>
        /// Select
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Attribute"></param>
        /// <param name="FuncOpetion"></param>
        /// <param name="Col"></param>
        /// <returns></returns>
        public string Select(string Title, string Attribute, Func<string> FuncOpetion, int Col = 6)
        {
            //<div class="col-sm-6 col-md-4">
            //    <h4 class="example-title">性别</h4>
            //    <select class="form-control" name="Member_Sex" data-bind="value:Member_Sex">
            //        <option value="">==请选择 性别==</option>
            //        @foreach (var item in sexList)
            //        {
            //            <option value="@item">@item</option>
            //        }
            //    </select>
            //</div>

            var Options = "==请选择 " + Title + "==";
            Options += FuncOpetion();

            var H4 = PageControl.H4(new Dictionary<string, string> { 
                {"class","example-title"}
            }, Title);

            var Select = PageControl.Select(ObjectToDictionary(Attribute), Options);

            var _Div_Attribute = new Dictionary<string, string>();
            _Div_Attribute.Add("class", "col-sm-" + Col);
            return PageControl.Div(_Div_Attribute, H4 + Select);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="options"> new { Text="",ID="",FindClick="",RemoveClick="" } </param>
        /// <param name="Readonly">【是否设置 文本框为 非只读】</param>
        /// <param name="KO">是否采用 KO 双向绑定值</param>
        /// <returns></returns>
        public string FindBack(object Options, bool Readonly = true, bool KO = true)
        {
            var di = new Dictionary<string, object>();

            Type ty = Options.GetType();

            var fields = ty.GetProperties().ToList();

            foreach (var item in fields)
            {
                di.Add(item.Name, item.GetValue(Options));
            }

            if (!di.ContainsKey("Text"))
                throw new Exception("查找带回控件缺少 Text 属性");
            if (!di.ContainsKey("ID"))
                throw new Exception("查找带回控件缺少 ID 属性");
            if (!di.ContainsKey("FindClick"))
                throw new Exception("查找带回控件缺少 FindClick 属性");
            if (!di.ContainsKey("RemoveClick"))
                throw new Exception("查找带回控件缺少 RemoveClick 属性");
            if (!di.ContainsKey("Placeholder"))
                throw new Exception("查找带回控件缺少 Placeholder 属性");

            var input_attr = new Dictionary<string, string>() { 
                {"type","text"},{ "class", "form-control" },{ "name", di["Text"].ToString() },{ "data-bind", "value:"+di["Text"].ToString() }
            };

            //ID 属性文本框
            var input_attr_id = new Dictionary<string, string>() { 
                {"type","text"},{ "class", "form-control" },{ "style", "display:none" },{ "name", di["ID"].ToString() },{ "data-bind", "value:"+di["ID"].ToString() }
            };

            //判断是否有 placeholder 属性
            if (di.ContainsKey("Placeholder"))
            {
                input_attr.Add("placeholder", di["Placeholder"].ToString());
            }

            //判断是否要设置 文本框的 readonly 属性
            if (Readonly)
            {
                input_attr.Add("readonly", "readonly");
            }

            //判断绑定值是否使用 KO 插件
            if (!KO)
            {
                input_attr.Remove("data-bind");
                input_attr.Add("value", "");
                input_attr_id.Remove("data-bind");
                input_attr_id.Add("value", "");
            }

            //Text 属性文本框
            var input = PageControl.Input(input_attr);

            var input_id = PageControl.Input(input_attr_id);

            //打开窗口按钮
            var find = PageControl.Button(
                    new Dictionary<string, string>() { 
                        {"type","button"},
                        {"class","btn btn-outline btn-default"},
                        {"onclick",di["FindClick"].ToString()  }
                    },
                    PageControl.I(new Dictionary<string, string>() { { "class", "fa fa-search" } }, "")
            );

            //清空信息按钮
            var close = PageControl.Button(
                    new Dictionary<string, string>() { 
                        {"type","button"},
                        {"class","btn btn-outline btn-default"},
                        {"onclick",di["RemoveClick"].ToString()}
                    },
                    PageControl.I(new Dictionary<string, string>() { { "class", "fa fa-close" } }, "")
            );

            //按钮组div
            var span = PageControl.Span(
                new Dictionary<string, string>() { 
                {"class","input-group-btn"}
            },
                find + close
            );


            //创建 查找带回Html
            var CreateHtml = PageControl.Div(
                new Dictionary<string, string>() { 
                {"class","input-group"}
            },
                input + input_id + span
            );

            return CreateHtml;
        }

        /// <summary>
        /// 上传 图片 控件
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Name"></param>
        /// <param name="Col"></param>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        /// <returns></returns>
        public string UploadImage(string Title, string Name, int Col = 6, int Width = 150, int Height = 150)
        {
            //<div class="col-sm-6 col-md-4">
            //    <h4 class="example-title">会员头像</h4>
            //    <div id="Member_Photo_Image" style="display:none; padding-bottom:5px;">
            //        <img data-bind="attr:{'src':Member_Photo}" width="150" height="150" style="border:1px solid red;" />
            //    </div>
            //    <button type="button" title="会员头像" onclick="$('#Member_Photo').click()" class="btn btn-primary btn-outline">
            //        <i class="fa fa-cloud-upload"></i>&nbsp;会员头像
            //    </button>
            //    <input type="file" accept="image/*" name="Member_Photo" id="Member_Photo" class="hide" onchange="model.Member_Photo(admin.getObjectUrl(this.files[0]))" />
            //    <label title="预览" data-bind="visible: Member_Photo" onclick="$('#Member_Photo_IMG').toggle(200)" class="btn btn-success btn-outline">
            //        <i class="fa fa-eye"></i>&nbsp;预览
            //    </label>
            //</div>


            var H4 = PageControl.H4(new Dictionary<string, string> { 
                {"class","example-title"}
            }, Title);

            var _Div_Image = PageControl.Div(
                new Dictionary<string, string>() { { "name", Name + "_Image" }, { "style", "display:none; padding-bottom:5px;" } },
                "<img data-bind=\"attr:{'src':" + Name + "}\" width=\"150\" height=\"150\" style=\"border:1px solid red;\" />"
                );

            var _Button = PageControl.Button(
                new Dictionary<string, string>() { { "type", "button" }, { "title", Title }, { "onclick", "$('#" + Name + "').click();" }, { "class", "btn btn-primary btn-outline" } },
                "<i class=\"fa fa-cloud-upload\"></i>&nbsp;" + Title + ""
                );

            var _File = "<input type=\"file\" accept=\"image/*\" name=\"" + Name + "\" id=\"" + Name + "\" class=\"hide\" onchange=\"model." + Name + "(admin.getObjectUrl(this.files[0]))\" />";

            var See = "<label title=\"预览\" data-bind=\"visible: " + Name + "\" onclick=\"$('div[name=" + Name + "_Image" + "]').toggle(200)\" class=\"btn btn-success btn-outline\"><i class=\"fa fa-eye\"></i>&nbsp;预览</label>";

            var _Div_Attribute = new Dictionary<string, string>();
            _Div_Attribute.Add("class", "col-sm-" + Col);
            return PageControl.Div(_Div_Attribute, H4 + _Div_Image + _Button + _File + See);
        }

        //public string UploadFile() { 



        //}

        /// <summary>
        /// UEditor 编辑器
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Name"></param>
        /// <param name="Col"></param>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        /// <returns></returns>
        public string UEditor(string Title, string Name, int Col = 6, string Width = "100%", string Height = "300px")
        {
            //<div class="col-sm-12">
            //    <h4 class="example-title">介绍</h4>
            //    <script id="Member_Introduce" type="text/plain" style="width:100%;height:300px;">
            //    </script>
            //</div>

            var H4 = PageControl.H4(new Dictionary<string, string> { 
                {"class","example-title"}
            }, Title);

            var _Script = PageControl.Script(new Dictionary<string, string>()
            {
                {"id",Name},
                {"type","text/plain"},
                {"style","width:"+Width+";height:"+Height+";"}
            }, "");

            var _Div_Attribute = new Dictionary<string, string>();
            _Div_Attribute.Add("class", "col-sm-" + Col);
            return PageControl.Div(_Div_Attribute, H4 + _Script);
        }





































    }
}
